package org.example.Thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class NestedThreadsExample {

    private static final AtomicBoolean stopAllThreads = new AtomicBoolean(false);


    private static  List<Integer> list = new ArrayList<>();
    private static Integer zero = 1;
    private static Integer zero1 = 1;

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(12);
        List<ExecutorService> innerExecutors = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            int finalI = i;
            executor.execute(() -> {
                if (!stopAllThreads.get()) {
                    System.out.println("Outer thread " + Thread.currentThread().getName() + " is running");
                    // 外部线程的任务代码，连接数据库等

                    ExecutorService innerExecutor = Executors.newFixedThreadPool(2);
                    innerExecutors.add(innerExecutor);
                    for (int j = 0; j < 2; j++) {
                        innerExecutor.execute(() -> {
                            if (!stopAllThreads.get()) {
                                System.out.println("Inner thread " + Thread.currentThread().getName() + " is running");

//                                try {
//                                    Thread.sleep(1000+(finalI *1000));
//                                } catch (InterruptedException ex) {
//                                    ex.printStackTrace();
//                                }
                                System.out.println("---------实际执行第"+ zero++ +"多少次");
//                                if (Math.random() < 0.1) { // 以10%的概率随机抛出异常来模拟错误情况
//                                    stopAllThreads.set(true);
//                                    System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
//                                }

                                if (finalI == 3) { // 以10%的概率随机抛出异常来模拟错误情况
                                    stopAllThreads.set(true);
                                    System.out.println("*** An error occured in inner thread " + Thread.currentThread().getName() + "! ***");
                                }


                                list.add(zero);
                                System.out.println("---------大小"+list.size());
                            }
                        });
                    }
                    innerExecutor.shutdown();
                }
            });
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            try {
                executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        for (ExecutorService innerExecutor : innerExecutors) {
            innerExecutor.shutdownNow();
            try {
                innerExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("*** All threads are stopped ***");
    }
}